%%%-------------------------------------------------------------------
%%% @author liuwentao
%%% @doc
%%%     单位数据装换
%%% @end
%%% Created : 26. 7月 2021 15:51
%%%-------------------------------------------------------------------
-module(unit_convert).
-author("liuwentao").

%% API
-export([
    to/2
]).

-include("common.hrl").
-include("unit.hrl").
-include("pos.hrl").
-include("role.hrl").

%% @doc 将#unit{}转换成其它数据格式
-spec to(Mode, Unit) -> {ok, tuple()} | {error, {unit_convert_failure, atom()}} when
    Mode :: atom() | tuple(),
    Unit :: {pos_integer(), pos_integer()} | #unit_data{} | #unit{}.
%% base数据转为单位数据
to(unit, {Type, UnitBaseId}) ->
    case unit_mgr:get_unit_data(Type, UnitBaseId) of
        UnitData = #unit_data{} ->
            to({unit, {0, 0, 0}, [], ""}, UnitData);
        _ ->
            {error, {unit_convert_failure, to_unit}}
    end;
to({unit, Pos, Evt, Disabled}, {Type, UnitBaseId}) when is_integer(UnitBaseId) ->
    case unit_mgr:get_unit_data(Type, UnitBaseId) of
        UnitData = #unit_data{} ->
            to({unit, Pos, Evt, Disabled}, UnitData);
        _ ->
            {error, {unit_convert_failure, to_unit}}
    end;
to({unit, {MapId, X, Y}, Evt, Disabled}, UnitData = #unit_data{}) ->
    to({unit, {MapId, X, Y, ?DIR_DEFAULT}, Evt, Disabled, []}, UnitData);
to({unit, {MapId, X, Y, Dir}, Evt, Disabled}, UnitData = #unit_data{}) ->
    to({unit, {MapId, X, Y, Dir}, Evt, Disabled, []}, UnitData);
to({unit, {MapId, X, Y}, Evt, Disabled, Path}, UnitData = #unit_data{}) ->
    to({unit, {MapId, X, Y, ?DIR_DEFAULT}, Evt, Disabled, Path}, UnitData);
to({unit, {MapId, X, Y, Dir}, Evt, Disabled, Path}, #unit_data{bid = BaseId, unit_type = UnitType, type = Type, fun_type = FunType, combat_type = CombatType, lev = Lev, status = Status, name = Name, condition = Cond,  speed = Speed, t_move = Tmove, t_atk = Tatk, range_trace = RangeTrace, range_atk = RangeAtk, talk = Talks, looks = Looks, data = Data, lock = Lock, def_status = DefStatus, activity = Activity, classes = Classes, sex = Sex, collect_times = CollectTimes, star_lev = _StarLev}) ->
    Pos = #pos{platform = env:get(platform), zone_id = env:get(zone_id), map = MapId, x = X, y = Y, dir = Dir},
    TmoveM = case Tmove > 1 of
                 true -> util:rand(Tmove * 1000, Tmove * 1000 + 1000);
                 false -> util:rand(3000, 5000) %% 默认3秒
             end,
    TatkM = case Tatk > 1 of
                true -> Tatk * 1000;
                false -> 3 * 1000
            end,
    NewExt = [],
    %% 取额外信息
    ExtMap = #{},
    NewLooks = [{Key, 0, Val, ""} || {Key, Val} <- Looks],
    {ok, #unit{base_id = BaseId, unit_type = UnitType, type = Type, fun_type = FunType, combat_type = CombatType, status = Status, lev = Lev,  name = Name, condition = Cond, speed = Speed, t_move = TmoveM, t_atk = TatkM, range_trace = RangeTrace, range_atk = RangeAtk, data = Data, looks = NewLooks, talk = Talks, lock = Lock, pos = Pos, evt = Evt, defxy = {MapId, X, Y}, disabled = Disabled, def_status = DefStatus, activity = Activity, sex = Sex, classes = Classes, collect_times = CollectTimes, path = Path, ext = NewExt}};

%% 转为#map_unit{}
to(map_unit, #unit{id = Id, unit_type = UnitType, type = Type, combat_type = CombatType,  status = Status, name = Name, base_id = BaseId,  speed = Speed, pos = #pos{x = X, y = Y, dir = Dir}, looks = Looks, activity = Activity,  sex = Sex, classes = Classes, prop = Prop}) ->
    {ok, #map_unit{id = Id, unit_type = UnitType, type = Type, combat_type = CombatType, status = Status, name = Name, base_id = BaseId, speed = Speed, x = X, y = Y, dir = Dir, looks = Looks, activity = Activity, sex = Sex, classes = Classes, prop = Prop}};


%% 无法转换
to(Type, Val) ->
    ?ERR("转换失败:~w:~w", [Type, Val]),
    {error, {unit_convert_failure, Type}}.



